package exe.ch3;

/**
 * @author oldliu
 * @since 1.0
 */
public class Market {
    String marketName;//商场名
    String marketAdress;//商场地址
    Memberc[] me = new Memberc[3];
    int memberNum = 0;//会员数量

    //新增会员方法
    public boolean add(Memberc c) {
        System.out.println(me);
        if (memberNum >= me.length) {
            System.out.println("数组长度不狗了，开始扩容");
            //1:新建一个更长的数组
            Memberc[] temp = new Memberc[me.length + 2];
            //2：把老数组的值复制过去
            for (int i = 0; i < me.length; i++) {
                temp[i] = me[i];
            }
            //3:保留新数组的值
            me = temp;
            //System.out.print("会员数已满，");
            //return false;
        }
        if (checkCardNoRepeat(c.getCardNum())) {
            return false;
        }
        me[memberNum] = c;
        memberNum++;
        return true;
    }

    //判断会员号重复方法
    public boolean checkCardNoRepeat(String no) {
        for (int i = 0; i < memberNum; i++) {
            if (me[i].getCardNum().equals(no)) {
                System.out.print("会员号重复，");
                return true;
            }
        }
        return false;
    }

    //销户
    public Memberc del(String cardNo) {
        for (int i = 0; i < memberNum; i++) {
            if (me[i].getCardNum().equals(cardNo)) {
                Memberc temp = me[i];//保留这个对象
                //移动
                for (int j = i; j < memberNum - 1; j++) {
                    me[j] = me[j + 1];
                }
                me[--memberNum] = null;
                return temp;
            }
        }
        return null;
    }
    //找最高的积分


    //加分方法
    public boolean inc(String cardNo, int fen) {
        for (int i = 0; i < memberNum; i++) {
            if (me[i].getCardNum().equals(cardNo)) {
                me[i].setIntegral(me[i].getIntegral() + fen);
                return true;
            }
        }
        return false;
    }

    //兑换礼品方法
    public String duihuan(String cardNo, int lipingleixing) {
        int deduct = 0;
        String lipin = null;
        switch (lipingleixing) {
            case 1:
                deduct = 100;
                lipin = "兑换餐巾纸一包";
                break;
            case 2:
                deduct = 400;
                lipin = "兑换剃须刀一把";
                break;
            case 3:
                deduct = 1000;
                lipin = "兑换炒锅一个";
                break;
        }
        for (int i = 0; i < memberNum; i++) {
            if (me[i].getCardNum().equals(cardNo)) {
                if (me[i].getIntegral() >= deduct) {
                    me[i].setIntegral(me[i].getIntegral() - deduct);
                    return lipin;
                }
                System.out.print("积分不够");
                return null;
            }
        }
        System.out.print("帐号不存在");

        return null;
    }

    //查看会员方法
    public void show() {
        System.out.println("名字是" + this.marketName + ",地址是" + this.marketAdress);
        for (int i = 0; i < memberNum; i++) {
            System.out.println(me[i]);
        }
    }
}
